package com.isoft.dailymanprovidor.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.dailymanprovidor.bean.ResponseData;
import com.isoft.dailymanprovidor.bean.ResultCode;
import com.isoft.dailymanprovidor.dao.MeetingReservationDao;
import com.isoft.dailymanprovidor.entity.MeetingReservation;
import com.isoft.dailymanprovidor.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class MeetingReservationService {
    @Autowired
    MeetingReservationDao meetingReservationDao;

    //分页获取会议室预约全部信息
    public ResponseData getallmeetingroom(Integer currentPage, Integer size){
        ResponseData responseData = new ResponseData();
        Page<MeetingReservation> meetingReservationPage=new Page<>();
        // 初始化分页参数
        if (currentPage == null){
            currentPage = 1;
        }
        if (size == null){
            size =10;
        }
        //构造查询条件
        meetingReservationPage.setCurrent(currentPage).setSize(size);
        QueryWrapper<MeetingReservation> wrapper=new QueryWrapper<>();

        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(meetingReservationDao.selectPage(meetingReservationPage,wrapper));
        return responseData;
    }
    //分页获取会议室预约信息
    public ResponseData getallmeetingreservation(Integer currentPage, Integer size,String meetingroomname,String department,String status,String starttime,String endtime,String applicant){
        ResponseData responseData = new ResponseData();
        Page<MeetingReservation> meetingReservationPage=new Page<>();
        // 初始化分页参数
        if (currentPage == null){
            currentPage = 1;
        }
        if (size == null){
            size =10;
        }
        //构造查询条件
        meetingReservationPage.setCurrent(currentPage).setSize(size);
        String start="2010-09-30T16:00:00.000Z";
        String end="2090-12-30T16:00:00.000Z";
        QueryWrapper wrapper=new QueryWrapper();
            wrapper.like("meetingroomname", meetingroomname);
        if(!department.equals("全部")){
            wrapper.eq("department",department);
        }
        if(!status.equals("全部")) {
            wrapper.eq("status", status);
        }
        if(starttime==null||starttime.isEmpty()) {
            wrapper.gt("starttime", start );
        }else {
            wrapper.gt("starttime", starttime);
        }
        if(endtime==null||endtime.isEmpty()) {
            wrapper.lt("endtime", end);
        }else {
            wrapper.lt("endtime", endtime);
        }
            wrapper.like("applicant",applicant);
        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(meetingReservationDao.selectPage(meetingReservationPage,wrapper));
        return responseData;
    }

    //新增会议室预约申请
    public boolean addmeetingreservation(MeetingReservation meetingReservation){
        return meetingReservationDao.addmeetingreservation(meetingReservation)>0;
    }
    //预约管理审核
    public boolean Manupdatestatue(MeetingReservation meetingReservation){
        return meetingReservationDao.Manupdatestatue(meetingReservation)>0;
    }
    //修改会议室预约信息
    public boolean updatemeetingroom(MeetingReservation meetingReservation){
        UpdateWrapper<MeetingReservation> wrapper=new UpdateWrapper<>();
        wrapper.set("department",meetingReservation.getDepartment());
        wrapper.set("meetingtitle",meetingReservation.getMeetingtitle());
        wrapper.set("meetingroomname",meetingReservation.getMeetingroomname());
        wrapper.set("starttime",meetingReservation.getStarttime());
        wrapper.set("endtime",meetingReservation.getEndtime());
        wrapper.set("participants",meetingReservation.getParticipants());
        wrapper.set("content",meetingReservation.getContent());
        wrapper.set("needlabel",meetingReservation.getNeedlabel());
        wrapper.set("labelcontent",meetingReservation.getLabelcontent());
        wrapper.set("meetingtype",meetingReservation.getMeetingtype());
        wrapper.set("applicant",meetingReservation.getApplicant());
        wrapper.set("screencasting",meetingReservation.getScreencasting());
        wrapper.set("notes",meetingReservation.getNotes());
        wrapper.set("materialupload",meetingReservation.getMaterialupload());
        wrapper.set("agendaupload",meetingReservation.getAgendaupload());
        wrapper.eq("id",meetingReservation.getId());
        wrapper.set("status",0);
        wrapper.eq("status",-1);
        return meetingReservationDao.update(wrapper)>0;
    }
    //撤销会议室预约信息
    public boolean deletemeetingroom(Integer id){
        return meetingReservationDao.deleteById(id)>0;
    }

}
